package controller;

import entity.Userinfo;
import service.serviceimpl.ServiceUserImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @Author wuchao66
 * @Date 2023/5/23 10:25
 * @PackageName:${PACKAGE_NAME}
 * @ClassName: ${NAME}
 * @Description: TODO
 * @Param
 * @Version 1.0
 */
@WebServlet(name = "ServletTest163",urlPatterns = "/ServletTest163")
public class ServletTest163 extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();//文件流
        //定义全局的op来判断curd,对应操作
        String op = request.getParameter("op");//先拿标志
        if (op != null) {//有标志，可以判断curd
            ServiceUserImpl serviceUser = new ServiceUserImpl();
            if (op.equals("delete")) {//做删除
                //拿到删除id
                int id= Integer.parseInt(request.getParameter("id"));
                //执行删除操作
                int i = serviceUser.executeUpdate("delete from userinfo where id=?", id);
                if(i>0){//删除成功
                    request.getRequestDispatcher("main.jsp?delete1=t").forward(request,response);
                }else{//删除失败
                    response.sendRedirect("main.jsp?delete2=t");
                }
            }
            else if(op.equals("add")){
                //着手增加
                String name = request.getParameter("name");
                String dept = request.getParameter("dept");
                int sex= Integer.parseInt(request.getParameter("sex"));
                int i=serviceUser.executeUpdate("insert into userinfo(name,sex,age,ranking,dept) values(?,?,18,'1',?) "
                        ,name,sex,dept);
                if(i>0){//增加成功
                    request.getRequestDispatcher("main.jsp?add1=t").forward(request,response);
                }else{//增加失败
                    response.sendRedirect("main.jsp?add2=t");
                }
            }else if(op.equals("update1")){
                //拿到传入要修改的id
                int id= Integer.parseInt(request.getParameter("id"));
                response.sendRedirect("update.jsp?id="+id);
            }else if(op.equals("update2")){
                int id= Integer.parseInt(request.getParameter("id"));
                String name = request.getParameter("name");
                String dept = request.getParameter("dept");
                int sex= Integer.parseInt(request.getParameter("sex"));
                int i = serviceUser.executeUpdate("update userinfo set name=?,sex=?,dept=? where id=?", name, sex, dept, id);
                if(i>0){//修改成功
                    request.getRequestDispatcher("main.jsp?update1=t").forward(request,response);
                }else{//修改失败
                    response.sendRedirect("main.jsp?update2=t");
                }

            }

        } else {//第一次来是没有op证明你就是第一次查询
           // show(request,response);
            ServiceUserImpl serviceUser = new ServiceUserImpl();
            List<Userinfo> userinfos = serviceUser.executeQuery("select * from userinfo");
            request.setAttribute("list",userinfos);
            userinfos.forEach(System.out::println);
            request.getRequestDispatcher("/jspnc1/main.jsp").forward(request,response);
        }

        out.flush();
        out.close();
    }
    public static void show(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        ServiceUserImpl serviceUser = new ServiceUserImpl();
        List<Userinfo> userinfos = serviceUser.executeQuery("select * from userinfo");
        request.setAttribute("list",userinfos);
        userinfos.forEach(System.out::println);
        request.getRequestDispatcher("/jspnc1/main.jsp").forward(request,response);
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();//文件流

        out.flush();
        out.close();
    }


}
